package controller.menus;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import javax.swing.JMenuItem;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;

import model.memento.MementoManager;

public class UndoMenuItemController implements ActionListener, ChangeListener {

	private final JMenuItem undoItem;
	// UndoManager
	private final MementoManager undoManager;
	
	public UndoMenuItemController(JMenuItem undoItem, MementoManager undoManager) {
		this.undoItem = undoItem;
		this.undoManager = undoManager;
		this.undoManager.addUndoableEditHappenedEventListener(this);
		this.undoItem.setEnabled(undoManager.canUndo());
	}
	
	@Override
	public void actionPerformed(ActionEvent e) {
		// undo compound action
		undoManager.undo();
	}

	/**
	 * @see javax.swing.event.ChangeListener#stateChanged(javax.swing.event.ChangeEvent)
	 */
	@Override
	public void stateChanged(ChangeEvent e) {
		undoItem.setEnabled(undoManager.canUndo());
	}
}
